package com.service.business.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.business.base.service.ConfigService;
import com.service.dao.constants.ConfigConstants;
import com.service.dao.enums.ProductEnums;
import com.service.dao.model.SysConfig;
import com.service.business.product.controller.rep.ProductOtherDetailRep;
import com.service.business.product.controller.rep.ProductOtherPageRep;
import com.service.business.product.service.ProductOtherService;
import com.service.business.support.Language;
import com.service.core.exception.CommonException;
import com.service.core.rest.request.PageRequest;
import com.service.core.rest.response.CommonPageRep;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.model.ProductOther;
import com.service.utility.modelmapper.ModelMapperUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.service.dao.mapper.ProductOtherMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("productDetailService")
public class ProductOtherServiceImpl extends ServiceImpl<ProductOtherMapper, ProductOther> implements ProductOtherService {

    @Autowired
    private ConfigService configService;

    @Override
    public CommonPageRep<ProductOtherPageRep> pageProduct(Integer categoryId, ProductEnums.Type productType, PageRequest pageRequest) {


        Map<String, SysConfig> configMap = configService.getConfigMapByGroup(ConfigConstants.Group.mall.getCode());

        String configName = ObjectUtils.nullSafeEquals(productType, ProductEnums.Type.other_gift) ?
                ConfigConstants.GIFT_ORDER_COINS_ID : ConfigConstants.POINTS_ORDER_COINS_ID;

        String giftCoinName = ObjectUtils.nullSafeEquals(productType, ProductEnums.Type.other_gift) ?
                ConfigConstants.GIFT_ORDER_GIFT_COINS : ConfigConstants.POINTS_ORDER_GIFT_COINS;

        SysConfig config = configMap.get(configName);

        SysConfig giftCoinConf = configMap.get(giftCoinName);

        Page<ProductOther> page = PageHelper.startPage(pageRequest.getPageNo(), pageRequest.getPageSize());
        list(new QueryWrapper<ProductOther>().lambda()
                .eq(ProductOther::getProductType, productType.getCode())
                .eq(ProductOther::getLanguage, Language.LANG.get())
                .eq(ProductOther::getCategoryId, categoryId)
                .eq(ProductOther::getProductStatus, CommonStatusEnums.ACTIVE.getCode())
                .orderByAsc(ProductOther::getSort));

        List<ProductOtherPageRep> resultList = page.getResult().stream()
                .map(item -> {
                    ProductOtherPageRep productOtherPageRep = ModelMapperUtil.getStrictModelMapper().map(item, ProductOtherPageRep.class);
                    productOtherPageRep.setBuyNum(item.getFakeBuyNum());
                    productOtherPageRep.setUnit(config.getConfOptions());
                    productOtherPageRep.setGiftCoin(giftCoinConf.getConfOptions());
                    return productOtherPageRep;
                }).collect(Collectors.toList());
        return CommonPageRep.buildResultRep(page, resultList);
    }

    @Override
    public ProductOtherDetailRep detail(Integer productId) {
        ProductOther productOther = baseMapper.selectById(productId);
        if (ObjectUtils.isEmpty(productOther)) {
            throw new CommonException("商品不存在", "product_not_exist");
        }

        Map<String, SysConfig> configMap = configService.getConfigMapByGroup(ConfigConstants.Group.mall.getCode());


        String configName = ObjectUtils.nullSafeEquals(productOther.getProductType(), ProductEnums.Type.other_gift.getCode()) ?
                ConfigConstants.GIFT_ORDER_COINS_ID : ConfigConstants.POINTS_ORDER_COINS_ID;


        ProductOtherDetailRep detailRep = ModelMapperUtil.getStrictModelMapper().map(productOther, ProductOtherDetailRep.class);
        detailRep.setBuyNum(productOther.getBuyNum());
        SysConfig config = configMap.get(configName);
        detailRep.setUnit(config.getConfOptions());

        config = configMap.get(ConfigConstants.FREIGHT_COINS_ID);
        detailRep.setFreightCoin(config.getConfOptions());

        String giftCoinName = ObjectUtils.nullSafeEquals(productOther.getProductType(), ProductEnums.Type.other_gift.getCode()) ?
                ConfigConstants.GIFT_ORDER_GIFT_COINS : ConfigConstants.POINTS_ORDER_GIFT_COINS;
        config = configMap.get(giftCoinName);
        detailRep.setGiftCoin(config.getConfOptions());

        return detailRep;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProductOtherNum(Integer productId, Integer sellNum) {

        ProductOther productOther = getById(productId);
        if (ObjectUtils.isEmpty(productOther)) {
            throw new CommonException("商品不存在", "product_not_exist");
        }
        Integer buyNum = productOther.getBuyNum() + sellNum;
        Integer fakeNum = productOther.getFakeBuyNum() + sellNum;
        Integer stock = productOther.getStock() - sellNum;
        ProductOther updateProduct = new ProductOther();
        updateProduct.setId(productId);
        updateProduct.setBuyNum(buyNum);
        updateProduct.setFakeBuyNum(fakeNum);
        if (productOther.getStock() > 0) {
            updateProduct.setStock(stock);
        }

        updateProduct.setPic(productOther.getPic());
        updateProduct.setPic1(productOther.getPic1());
        updateProduct.setPic2(productOther.getPic2());
        updateProduct.setPic3(productOther.getPic3());
        updateProduct.setPic4(productOther.getPic4());
        updateProduct.setPic5(productOther.getPic5());
        updateById(updateProduct);
    }


}
